package com.zhang.remoteapi.config;

import feign.RequestInterceptor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;

/**
 * @Description feign 拦截器传递 header 中oauth token，
 * @Author zsw
 * @Date 2019/10/30 9:52
 * @Version V1.0
 **/
@Configuration
@Slf4j
@AllArgsConstructor
@ConditionalOnProperty("security.oauth2.client.client-id")
public class FeignClientConfiguration {

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext,
                                                            OAuth2ProtectedResourceDetails resource,
                                                            AccessTokenContextRelay accessTokenContextRelay) {
        log.info("调用方法--》com.pig4cloud.pig.common.security.feign.PigFeignClientConfiguration");
        return new FeignClientInterceptor(oAuth2ClientContext, resource, accessTokenContextRelay);
    }
}